package com.st.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public abstract class BaseServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码
		//request.setCharacterEncoding("utf-8");
		//response.setContentType("text/html; charset=utf-8");
		
		// 获取action
		String action = request.getParameter("action");
	
		// 根据action 调用相应的方法
		// 反射查找方法
		Class cls = this.getClass();
		
		try {
			// 根据method 获取相应的方法
			Method method = cls.getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
		
			// 调用这个方法完成响应
			method.invoke(this, request, response);
			
		} catch (NoSuchMethodException e) {
			// 你的Servlet中没有实现相应的方法
			response.getWriter().write("你的Servlet中没有实现相应的方法");		
			e.printStackTrace();
		} catch (SecurityException e) {
			response.getWriter().write("你的Servlet中的方法, 无法访问");
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			response.getWriter().write("你的Servlet中的方法, 无法访问1");
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			response.getWriter().write("你的Servlet中的方法, 无法访问2");
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			response.getWriter().write("你的Servlet中的方法, 无法访问3");
			e.printStackTrace();
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
